<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/autogenerator.php';

class AutogeneratorModelXml_type extends JModelAdmin {

    protected $text_prefix = 'COM_AUTOGENERATOR';

    public function getForm($data = array(), $loadData = true) {
        $form = $this->loadForm('com_autogenerator.xml_type', 'xml_type', array('control' => 'jform', 'load_data' => $loadData));

        if (empty($form)) {
            return false;
        }

        return $form;
    }

    public function getTable($type = 'Xml_type', $prefix = 'AutogeneratorTable', $config = array()) {
        return JTable::getInstance($type, $prefix, $config);
    }

    protected function loadFormData() {
        $data = JFactory::getApplication()->getUserState('com_autogenerator.edit.xml_type.data', array());

        if (empty($data)) {
            $data = $this->getItem();
        }

        return $data;
    }

    protected function getReorderConditions($table) {
        $condition = array();

        return $condition;
    }

    protected function prepareTable($table) {
        if (empty($table->id)) {
            if (empty($table->ordering)) {
                $db = JFactory::getDbo();
                $db->setQuery('SELECT MAX(ordering) FROM #__auto_xml_type');
                $max = $db->loadResult();

                $table->ordering = $max + 1;
            }
        }
    }
    
    /**
     * For ajax.
     */
    public function changeType($iXMLTypeId) {
        $iKey = (int) $iXMLTypeId;
        if ($iKey < 1) {
            return JText::_('COM_AUTOGENERATOR_TABLE_AJAX_ERROR_MESSAGE_TABLE_NOT_VALID');
        }

        $oTable = $this->getTable();

        // Allow an exception to be thrown.
        try {
            // Load the row if saving an existing record.
            if ($iKey > 0) {
                if (!$oTable->load($iKey)) {
                    return JText::_('COM_AUTOGENERATOR_TABLE_AJAX_ERROR_MESSAGE_TABLE_NOT_VALID');
                }
            }

            if (intval($oTable->id) == 0) {
                return JText::_('COM_AUTOGENERATOR_TABLE_AJAX_ERROR_MESSAGE_TABLE_NOT_VALID');
            }

            // Change the status.
            if ($oTable->type == 1)
                $oTable->type = 0;
            else
                $oTable->type = 1;

            // Prepare the row for saving
            $this->prepareTable($oTable);

            // Check the data.
            if (!$oTable->check()) {
                return $oTable->getError();
            }

            // Store the data.
            if (!$oTable->store()) {
                return $oTable->getError();
            }

            // Clean the cache.
            $this->cleanCache();
        } catch (Exception $e) {
            return $e->getMessage();
        }

        return $oTable->type;
    }

}
